﻿Public Class FrmTimThongTinTheoXa
    Private Sub FrmTimThongTinTheoXa_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadHuyen(cmbHuyen)
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
        'Giam Tu ngay di 1 nam
        dtTuNgay.Text = DateSerial(Date.Today.Year - 1, Date.Today.Month, Date.Today.Day)

        dgvDSCongTrinh.RowsDefaultCellStyle.BackColor = Color.Beige
        dgvDSCongTrinh.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub

    Private Sub btnTimKiem_Click(sender As Object, e As EventArgs) Handles btnTimKiem.Click
        Dim TuNgay As String
        TuNgay = String.Format("{0:MM/dd/yyyy}", dtTuNgay.Value)
        Dim DenNgay As String
        DenNgay = String.Format("{0:MM/dd/yyyy}", dtDenNgay.Value)

        If TuNgay > DenNgay Then
            MessageBox.Show("Từ ngày phải nhỏ hơn Đến ngày", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        Dim sql As String
        sql = "SELECT CONGTRINH.SoHoSo, CONGTRINH.TenCongTrinh, CONVERT(char(10), CONGTRINH.NgayNhan, 103) AS NgayNhan, CONGTRINH.HoSoNam, CONGTRINH.ThanhPhanHoSo, " &
              "TENHUYEN.T_Huyen, TENXA.TenXa, DMPhong.TenPhong, DMKe.TenKe, DMKho.TenKho, DMTinhTrang.TenTinhTrang " &
              "FROM CONGTRINH LEFT OUTER JOIN " &
              "DMPhong ON congtrinh.MaPhongID = DMPhong.MaPhongID LEFT OUTER JOIN " &
              "DMKe ON CONGTRINH.MaKeID = DMKe.MaKeID LEFT OUTER JOIN " &
              "DMKho ON CONGTRINH.MaKhoID = DMKho.maKhoID LEFT OUTER JOIN " &
              "DMTinhTrang ON CONGTRINH.MaTinhTrangID = DMTinhTrang.MaTinhTrangID LEFT OUTER JOIN " &
              "TENXA ON CONGTRINH.MaXa = TENXA.MaXa LEFT OUTER JOIN " &
              "TENHUYEN ON TENHUYEN.maH = TENXA.MaHuyen " &
              "WHERE TENXA.MAXA = " & Int32.Parse(cmbXa.SelectedValue) & "" &
              "AND CONGTRINH.NgayNhan BETWEEN '" & TuNgay & "' AND '" & DenNgay & "'"

        dgvDSCongTrinh.DataSource = LoadThongTin(sql)
        dgvDSCongTrinh.Refresh()

        If dgvDSCongTrinh.Rows.Count = 0 Then
            MessageBox.Show("Không tìm thấy thông tin  ", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

    Private Sub btnThoat_Click(sender As Object, e As EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub
    Private Sub btnXuatExcel_Click(sender As Object, e As EventArgs) Handles btnXuatExcel.Click
        ExportExcel(dgvDSCongTrinh)
    End Sub

    Private Sub cmbHuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbHuyen.SelectedIndexChanged
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
    End Sub

    Private Sub dgvDSCongTrinh_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvDSCongTrinh.RowPostPaint
        ' xuat so thu tu tu dong cho datagridview
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < dgvDSCongTrinh.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If dgvDSCongTrinh.RowHeadersWidth < Size.Width + 20 Then dgvDSCongTrinh.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
End Class